package com.zpkj.portal.third.jpush;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.common.resp.DefaultResult;
import cn.jpush.api.device.AliasDeviceListResult;
import cn.jpush.api.device.TagAliasResult;


/**
 * 极光推送 设备管理类
 * @author xc
 */
public class JPushDeviceUtils {
	protected static final Logger LOG = LoggerFactory.getLogger(JPushDeviceUtils.class);

	private static final String appKey = "2550849cefa15cca96e46b61";
	private static final String masterSecret = "70baf120d892f60b13b560ab";
//	private static final String TAG1 = "tag1";
//	private static final String ALIAS1 = "alias1";
//	private static final String ALIAS2 = "alias2";
//	private static final String REGISTRATION_ID = "1a0018970aa527703ce";
//	private static final String REGISTRATION_ID2 = "0a04ad7d8b4";

	private static JPushClient jpushClient = new JPushClient(masterSecret, appKey);

//	public static void main(String[] args) throws Exception{
//		Set<String> tagsToAdd = new HashSet<String>(); 
//		tagsToAdd.add("tag1");
////		Set<String> tagsToRemove = new HashSet<String>();
////		tagsToRemove.add("tag1");
//		String alias = "adf2468c589b4ccea3b920f48f532831";
//		//设置别名，添加/删除 标签
////		JPushDeviceUtils.testUpdateDeviceTagAlias_add_remove_tags(REGISTRATION_ID, alias, null, null);
//		//清理设备信息
////		testUpdateDeviceTagAlias_clear(REGISTRATION_ID);
//		getDeviceTagAlias(REGISTRATION_ID);
//	}
	
	/**
	 * 修改别名和添加组、删除组
	 */
	public static void testUpdateDeviceTagAlias_add_remove_tags(
			String registration_id,String alias,
			Set<String> tagsToAdd,Set<String> tagsToRemove) throws APIConnectionException, APIRequestException {
		DefaultResult result = jpushClient.updateDeviceTagAlias(registration_id, alias, tagsToAdd, tagsToRemove);
	}
	
	/**
	 * 根据设备id获取别名、标签信息
	 */
	public static void getDeviceTagAlias(String registionId) throws APIConnectionException, APIRequestException {
		TagAliasResult result = jpushClient.getDeviceTagAlias(registionId);
		LOG.info(result.alias);
		LOG.info(result.tags.toString());
	}

	/**
	 * 获取用户在线状态
	 */
//	public static void testGetUserOnlineStatus() {
//		try {
//			Map<String, OnlineStatus> result =  jpushClient.getUserOnlineStatus(REGISTRATION_ID1, REGISTRATION_ID2);
//
//			LOG.info(result.get(REGISTRATION_ID1).toString());
//			LOG.info(result.get(REGISTRATION_ID2).toString());
//		} catch (APIConnectionException e) {
//			LOG.error("Connection error. Should retry later. ", e);
//		} catch (APIRequestException e) {
//			LOG.error("Error response from JPush server. Should review and fix it. ", e);
//			LOG.info("HTTP Status: " + e.getStatus());
//			LOG.info("Error Code: " + e.getErrorCode());
//			LOG.info("Error Message: " + e.getErrorMessage());
//		}
//	}
	
	
	/**
	 * 根据别名获取设备信息
	 * @throws APIRequestException 
	 * @throws APIConnectionException 
	 */
//	public static void getAliasDeviceList(String alias) throws APIConnectionException, APIRequestException {
//		AliasDeviceListResult result = jpushClient.getAliasDeviceList(alias, null);
//	}
	public static List<String> getAliasDeviceList(String alias) throws APIConnectionException, APIRequestException {
		AliasDeviceListResult result = jpushClient.getAliasDeviceList(alias, null);
		List<String> list = result.registration_ids;
		return list;
	}

	/**
	 * 绑定手机号
	 */
//	public static void testBindMobile() {
//		try {
//			DefaultResult result =  jpushClient.bindMobile(REGISTRATION_ID1, "13000000000");
//			LOG.info("Got result " + result);
//		} catch (APIConnectionException e) {
//			LOG.error("Connection error. Should retry later. ", e);
//		} catch (APIRequestException e) {
//			LOG.error("Error response from JPush server. Should review and fix it. ", e);
//			LOG.info("HTTP Status: " + e.getStatus());
//			LOG.info("Error Code: " + e.getErrorCode());
//			LOG.info("Error Message: " + e.getErrorMessage());
//		}
//	}
	
//	
//	@Test
//	@TestOrder(order = 110)
//	public void testGetDeviceTagAlias_1() throws Exception {
//		TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1);
//		
//		assertTrue(result.isResultOK());
//		assertEquals("alias not equals", "alias1", result.alias);
//		
//		assertTrue("tag contains", result.tags.contains("tag1"));
//		assertTrue("tag contains", result.tags.contains("tag2"));
//		assertFalse("tag not contains", result.tags.contains("tag3"));
//		assertFalse("tag not contains", result.tags.contains("tag4"));
//	}
//	@Test
//	@TestOrder(order = 111)
//	public void testGetAliasDeviceList_1() throws APIConnectionException, APIRequestException {
//		AliasDeviceListResult result = jpushClient.getAliasDeviceList("alias1", null);
//		assertTrue(result.registration_ids.contains(REGISTRATION_ID1));
//	}
//	
//	@Test
//	@TestOrder(order = 120)
	//清理设备的标签和别名
	public static void testUpdateDeviceTagAlias_clear(String registionId) throws APIConnectionException, APIRequestException {
		DefaultResult result = jpushClient.updateDeviceTagAlias(registionId, true, true);
	}
//	
//	@Test
//	@TestOrder(order = 130)
//	public void testGetDeviceTagAlias_cleard() throws Exception {
//		TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1);
//		
//		assertTrue(result.isResultOK());
//		assertEquals("alias cleared", null, result.alias);
//		assertEquals("tags cleared", 0, result.tags.size());
//	}
//
//	
//	
//	// ------------------ tags
//
//	@Test
//	@TestOrder(order = 203)
	/**
	 * 根据标签删除/添加设备
	 * @throws APIConnectionException
	 * @throws APIRequestException
	 */
//	public void testAddRemoveDevicesFromTag() throws APIConnectionException, APIRequestException {
//		Set<String> toAddUsers  = new HashSet<String>();
//		toAddUsers.add(REGISTRATION_ID1);
//		Set<String> toRemoveUsers  = new HashSet<String>();
//		toRemoveUsers.add(REGISTRATION_ID2);
//		DefaultResult result = jpushClient.addRemoveDevicesFromTag("tag3", toAddUsers, toRemoveUsers);
//		assertTrue(result.isResultOK());
//	}
//	
//	@Test
//	@TestOrder(order = 210)
//	public void testIsDeviceInTag() throws APIConnectionException, APIRequestException {
//		BooleanResult result = jpushClient.isDeviceInTag("tag3", REGISTRATION_ID1);
//		assertTrue("", result.result);
//		result = jpushClient.isDeviceInTag("tag3", REGISTRATION_ID2);
//		assertFalse("", result.result);
//	}
//
//	@Test
//	@TestOrder(order = 211)
//	public void testAddRemoveDevicesFromTagResult() throws APIConnectionException, APIRequestException {
//		TagListResult result = jpushClient.getTagList();
//		assertTrue("", result.tags.contains("tag3"));
//	}
//	
//	@Test
//	@TestOrder(order = 220)
//	public void testGetTagList_1() throws Exception {
//		TagListResult result = jpushClient.getTagList();
//		assertTrue("", result.tags.size() > 0);
//	}
//	
//	@Test
//	@TestOrder(order = 250)
	/**
	 * 删除标签
	 * @throws APIConnectionException
	 * @throws APIRequestException
	 */
	public void testDeleteTag(String tag) throws APIConnectionException, APIRequestException {
		DefaultResult result = jpushClient.deleteTag(tag, null);
	}
//
//	@Test
//	@TestOrder(order = 251)
//	public void testDeleteResult() throws APIConnectionException, APIRequestException {
//		TagListResult result = jpushClient.getTagList();
//		assertFalse("", result.tags.contains("tag3"));
//	}
//	
//	@Test
//	@TestOrder(order = 260)
//	public void testDeletetag2() throws APIConnectionException, APIRequestException {
//		DefaultResult result = jpushClient.deleteTag("tag3", null);
//		assertTrue(result.isResultOK());
//	}
//	
//	// --------------------- alias
//	
//	@Test
//	@TestOrder(order = 230)
//	public void testGetAliasDevices_1() throws Exception {
//		AliasDeviceListResult result = jpushClient.getAliasDeviceList("alias1", null);
//		assertTrue(result.isResultOK());
//	}
//
//	@Test
//	@TestOrder(order = 300)
//	public void testGetAliasDeviceList() throws APIConnectionException, APIRequestException {
//		AliasDeviceListResult result = jpushClient.getAliasDeviceList("alias1", "android");
//		assertTrue(result.isResultOK());
//	}
//	
//	@Test
//	@TestOrder(order = 310)
	/**
	 * 根据别名获取设备列表
	 */
	public void getAliasDeviceList() throws APIConnectionException, APIRequestException {
		AliasDeviceListResult result = jpushClient.getAliasDeviceList("alias1", null);
		List<String> list = result.registration_ids;
		for( String str:list ){
		}
	}
//	
//	@Test
//	@TestOrder(order = 320)
//	public void testDeleteAlias() throws APIConnectionException, APIRequestException {
//		DefaultResult result = jpushClient.deleteAlias("alias2", "android");
//		assertTrue(result.isResultOK());
//	}
//	
//	
//	@Test
//	@TestOrder(order = 330)
	/**
	 * 删除别名
	 */
	public void testDeleteAlias_2(String alias) throws APIConnectionException, APIRequestException {
		DefaultResult result = jpushClient.deleteAlias(alias, null);
	}
//
//	@Test
//	@TestOrder(order = 340)
//	public void testTetUserOnlineStatus() throws APIConnectionException, APIRequestException{
//		Map<String, OnlineStatus> result =  jpushClient.getUserOnlineStatus(REGISTRATION_ID1, REGISTRATION_ID2);
//		assertTrue(result.get(REGISTRATION_ID1) != null);
//	}
//
//	@Test
//	@TestOrder(order = 360)
//	public void testBindMobile() throws APIConnectionException, APIRequestException {
//		DefaultResult result = jpushClient.bindMobile(REGISTRATION_ID1, "13000000000");
//		assertTrue(result.isResultOK());
//	}
//
//	@Test
//	@TestOrder(order = 361)
//	public void testBindMobile_null() throws APIConnectionException, APIRequestException {
//		DefaultResult result = jpushClient.bindMobile(REGISTRATION_ID1, null);
//		assertTrue(result.isResultOK());
//	}
//
//	@Test
//	@TestOrder(order = 362)
//	public void testBindMobile_empty() throws APIConnectionException, APIRequestException {
//		DefaultResult result = jpushClient.bindMobile(REGISTRATION_ID1, "");
//		assertTrue(result.isResultOK());
//	}


	
}